<?php
require_once 'include/amazon_api_class.php';
require_once 'include/functions.php';
require_once 'settings.php';
if (isset($_GET['getxml']))
{
    require_once 'include/pclzip.lib.php';
    $project = "xml/" . trim($_GET['getxml']) . ".xml";
    $xml = file_get_contents($project);
    $headxml = "<?xml version='1.0' encoding='UTF-8'?>";
    $headxml .= '<ns0:feed xmlns:ns0="http://www.w3.org/2005/Atom">';
    $headxml .= '<ns0:title type="html">' . trim($_GET['getxml']) . '</ns0:title>';
    $headxml .= '<ns0:generator>Blogger</ns0:generator>';
    $headxml .= '<ns0:link href="http://' . $_SERVER['HTTP_HOST'] .
        '" rel="self" type="application/atom+xml" />';
    $headxml .= '<ns0:link href="http://' . $_SERVER['HTTP_HOST'] .
        '" rel="alternate" type="text/html" />';
    $headxml .= '<ns0:updated>' . date('Y-m-d') . 'T' . date('H:i:s') .
        'Z</ns0:updated>';
    $footxml = '</ns0:feed>';

    if (strpos($xml, "<?xml version='1.0' encoding='UTF-8'?>") === false)
    {
        $xmldata = $headxml . $xml . $footxml;
        $xmo = fopen($project, 'w');
        fputs($xmo, $xmldata);
        fclose($xmo);

        $updateasin = file_get_contents(trim($_GET['getxml']) . "_asin.txt");
        $fsin = fopen("asinlist.txt", "w");
        fputs($fsin, trim($updateasin));
        fclose($fsin);

        unlink(trim($_GET['getxml']) . "_asin.txt");
    }
    if(file_exists('xml/'.trim($_GET['getxml']).'.zip')) unlink('xml/'.trim($_GET['getxml']).'.zip');
    $archive = new PclZip('xml/'.trim($_GET['getxml']).'.zip');
    $v_list = $archive->add('xml/'.trim($_GET['getxml']).'.xml',PCLZIP_OPT_REMOVE_ALL_PATH);
   
    if(file_exists('xml/'.trim($_GET['getxml']).'.zip')) {
        unlink('xml/'.trim($_GET['getxml']).'.xml');
        header("location:xml/".trim($_GET['getxml']).".zip");
    } else {
        header("location:xml/".trim($_GET['getxml']).".xml");
    }

} else
{

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta name="author" content="Miftahul Firdaus" />

	<title>Blogger XML Builder</title>
<!-- jQuery -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<link rel="stylesheet" media="screen" type="text/css" href="datepicker/css/datepicker.css" />
<script type="text/javascript" src="datepicker/js/datepicker.js"></script>
<style>
body{
    font-family:arial;
    font-size:13px;
    background-color:#878787;

}
.xmlbuilder{
    background-color:#fff;
    width:320px;
    padding:10px;
    text-align:left;
    position:relative;
    height:340px;
}
.calender{
    width:220px;
    overflow-y:scroll;
}
#clear{
    height:20px;
}
#submit{
    position:absolute;
    top:311px;
    left:10px;
}

</style>    
</head>

<body>
<?php

    if (isset($_POST))
    {
        $waiting = trim($_POST['waiting']);
        $exasin = trim($_POST['exasin']);
    } else
    {
        $waiting = 5;
        $exasin = 3;
    }


    if (isset($_POST['project']))
    {
        if ($_POST['project'] == "")
            die('<span style="color:#ffffff">Please enter project name</span>');
        $projectasin = trim($_POST['project']) . "_asin.txt";
        if ($_POST['selectedrange'] == "")
            die('<span style="color:#ffffff">Please choose date range</span>');
        if (file_exists('asinlist.txt') && isset($_POST['initial']))
        {

            $asinlist = file_get_contents('asinlist.txt');
            $list = explode("\n", trim($asinlist));
            if (count($list) >= 1)
            {

                $fo = fopen($projectasin, "w");
                fputs($fo, $asinlist);
                fclose($fo);
            } else
            {
                die('<span style="color:#ffffff">Please run <a href="asingrabber.php">asingrabber</a> to populate ASIN</span>');
            }
        }
        if (!file_exists($projectasin))
            die('The job is halted, check your xml file <a href="xml/' . trim($_POST['project']) .
                '.xml">here</a>');
        $obj_amazon = new AmazonProductAPI();
        $obj_amazon->setAssociateTag($associateid);
        $obj_amazon->setPublicKey($amazonaccesskey);
        $obj_amazon->setPrivateKey($amazonsecretkey);
        $obj_amazon->setRegion($region);
        
        $next = true;
        if (isset($_POST['initial'])) $exasin =1;
        for ($n = 0; $n < $exasin; $n++)
        {
            $asinlist = file_get_contents($projectasin);
            if (strlen(trim($asinlist)) < 1){
                echo '<div align="center"><span style="color:#ffffff">All ASINs are executed! Click <a href="?getxml=' .
                    trim($_POST['project']) . '">here</a> to download the xml file</span></div>';
                $next = false;    
                break;
                exit;    
            }
            
            $list = explode("\n", trim($asinlist)); 
            $search = $list[0];


            try
            {
                $xml = $obj_amazon->getItemByAsin($search);
            }
            catch (exception $e)
            {
                //echo 'Caught exception: ',  $e->getMessage(), "\n";
                $asinlist = str_replace($search, '', $asinlist);
                $asinlist = trim($asinlist);
                $op = fopen($projectasin, "w");
                fputs($op, $asinlist);
                fclose($op);
                showReload();
                die();
            }
            $item = $xml->Items->Item;
            $detailurl = $item->DetailPageURL;
            $technicaldetailsurl = "";
            if (isset($item->ItemLinks->ItemLink[0]->URL))
                $technicaldetailsurl = $item->ItemLinks->ItemLink[0]->URL;
            $customersreviewsurl = "";
            if (isset($item->ItemLinks->ItemLink[5]->URL))
                $customersreviewsurl = $item->ItemLinks->ItemLink[5]->URL;

            $salesrank = "";
            if (isset($item->SalesRank))
                $salesrank = $item->SalesRank;
            $smallimagesrc = "";
            if (isset($item->SmallImage->URL))
                $smallimagesrc = $item->SmallImage->URL;
            $mediumimagesrc = "";
            if (isset($item->MediumImage->URL))
                $mediumimagesrc = $item->MediumImage->URL;
            $largeimagesrc = "";
            if (isset($item->LargeImage->URL))
                $largeimagesrc = $item->LargeImage->URL;


            if ($smallimagesrc == "")
            {
                if ($mediumimagesrc != "")
                {
                    $smallimagesrc = $mediumimagesrc;
                } elseif ($mediumimagesrc == "" && $largeimagesrc != "")
                {
                    $smallimagesrc = $largeimagesrc;
                }
            }


            if ($mediumimagesrc == "")
            {
                if ($smallimagesrc != "")
                {
                    $mediumimagesrc = $smallimagesrc;
                } elseif ($smallimage == "" && $largeimage != "")
                {
                    $mediumimagesrc = $largeimagesrc;
                }
            }

            if ($largeimagesrc == "")
            {
                if ($mediumimagesrc != "")
                {
                    $largeimagesrc = $mediumimagesrc;
                } elseif ($mediumimagesrc == "" && $smallimagesrc != "")
                {
                    $largeimagesrc = $smallimagesrc;
                }
            }

            $smallimage = "";
            $mediumimage = "";
            $largeimage = "";

            if ($smallimagesrc != "")
                $smallimage = "<img src=\"" . $smallimagesrc . "\" />\n";
            if ($mediumimagesrc != "")
                $mediumimage = "<img src=\"" . $mediumimagesrc . "\" />\n";
            if ($largeimagesrc != "")
                $largeimage = "<img src=\"" . $largeimagesrc . "\" />\n";

            $detailbrand = "";
            if (isset($item->ItemAttributes->Brand))
                $detailbrand = $item->ItemAttributes->Brand;
            $detailcolor = "";
            if (isset($item->ItemAttributes->Color))
                $detailcolor = $item->ItemAttributes->Color;
            $featured = "";
            if (isset($item->ItemAttributes->Feature))
            {
                $feature = $item->ItemAttributes->Feature;


                if (count($feature) >= 1)
                {
                    $featured = "<strong>Features " . $title . "</strong><br />\n<ul>";
                    for ($i = 0; $i < count($feature); $i++)
                    {
                        $featured .= "<li>" . $feature[$i] . "</li>\n";
                    }
                    $featured .= "</ul>";

                }
            }

            $listprice = "";
            if (isset($item->ItemAttributes->ListPrice->FormattedPrice))
                $listprice = $item->ItemAttributes->ListPrice->FormattedPrice;
            $title = $item->ItemAttributes->Title;

            $offersurl = "";
            if (isset($item->Offers->MoreOffersUrl))
                $offersurl = $item->Offers->MoreOffersUrl;

            $productdescription = "";
            $reviewtitle = "";
            $reviewcontent = "";
            if (isset($item->EditorialReviews->EditorialReview[0]->Source))
                $reviewtitle = $item->EditorialReviews->EditorialReview[0]->Source;
            if (isset($item->EditorialReviews->EditorialReview[0]->Content))
                $reviewcontent = $item->EditorialReviews->EditorialReview[0]->Content;
            $amazonreviewtitle = "";
            $amazonreviewcontent = "";
            if (isset($item->EditorialReviews->EditorialReview[1]->Source))
                $amazonreviewtitle = $item->EditorialReviews->EditorialReview[1]->Source;
            if (isset($item->EditorialReviews->EditorialReview[1]->Content))
                $amazonreviewcontent = $item->EditorialReviews->EditorialReview[1]->Content;

            if ($amazonreviewcontent != "")
            {
                $productdescription = "<strong>" . $amazonreviewtitle . "</strong><br />\n" . $amazonreviewcontent;
            } else
            {
                $productdescription = "<strong>" . $reviewtitle . "</strong><br />\n" . $reviewcontent;
            }

            if (strlen($productdescription) >= 0)
            {
                $productdescription = strip_tags($productdescription, "<strong><p><img><br><b>");
                $similiar = "";
                if (isset($item->SimilarProducts->SimilarProduct))
                    $similiar = $item->SimilarProducts->SimilarProduct;
                $tag = array();
                $mask = array();
                preg_match_all("/<img(.*?)>/", $productdescription, $img);
                for ($i = 0; $i < count($img[0]); $i++)
                {
                    $tag[] = $img[0][$i];
                    $mask[] = base64_encode($img[0][$i]);
                }
                $productdescription = str_replace($tag, $mask, $productdescription);

                $productdescription = str_replace(array("<p>", "</p>", "<br />", "<strong>",
                    "</strong>"), array(base64_encode("<p>"), base64_encode("</p>"), base64_encode("<br />"),
                    base64_encode("<strong>"), base64_encode("</strong>")), $productdescription);
                $productdescription = string_limit_words($productdescription, 1000);

                $productdescription = str_replace($mask, $tag, $productdescription);
                
                $productdescription = str_replace(array(base64_encode("<p>"), base64_encode("</p>"),
                    base64_encode("<br />"), base64_encode("<strong>"), base64_encode("</strong>")),
                    array("<p>", "</p>", "<br />", "<strong>", "</strong>"), $productdescription);
                    
                $productdescription = preg_replace("/<img(.*?)>/", "<br />\n$0\n<br />", $productdescription);
                $productdescriptionclean = preg_replace("/<img(.*?)>/", "", $productdescription);


                $relatedproducts = "";
                if (count($similiar) >= 1)
                {
                    if (strlen(trim($similiar[0]->Title)) >= 10)
                    {
                        $relatedproducts = "<strong>Related Products</strong>\n<br />\n<ul>\n";
                        for ($i = 0; $i < count($similiar); $i++)
                        {
                            $relatedproducts .= "<li> <dips elms='simil/" . $similiar[$i]->
                                ASIN . "/simil'>" . $similiar[$i]->
                                Title . "</dips><dips elms='simil//simil'></dips></li>\n";
                        }
                        $relatedproducts .= "</ul>";
                    }

                }
                
                $relatedproductsrev = "";
                if (count($similiar) >= 1)
                {
                    if (strlen(trim($similiar[0]->Title)) >= 10)
                    {
                        $relatedproductsrev = "<strong>Related Products</strong>\n<br />\n<ul>\n";
                        for ($i = 0; $i < count($similiar); $i++)
                        {
                            $cleanuri = strrev("http://www.amazon.".$region."/exec/obidos/ASIN/" . $similiar[$i]-> ASIN . "/" . $associateid);
                            $relatedproductsrev .= "<li> <dips id=\"fgtr\" elms=\"".$cleanuri."\" >" . $similiar[$i]->
                                Title . "</dips></li>\n";
                        }
                        $relatedproductsrev .= "</ul>";
                    }

                }
				$urlasin = ("http://www.amazon.".$region."/exec/obidos/ASIN/" . $item->ASIN . "/" . $associateid);
				$urlasinrev = strrev("http://www.amazon.".$region."/exec/obidos/ASIN/" . $item->ASIN . "/" . $associateid);
				
              

                $temp = explode("[#]", file_get_contents('template.txt'));
                $asin = $item->ASIN;
                $asintitle = ".$title.".$item->ASIN;
                
               	$template = str_replace('{asintitle}', $asintitle , $temp[1]);
                $template = str_replace('{title}', $title , $temp[1]);
                $template = str_replace('{detailurl}', $detailurl, $template);
                $template = str_replace('{smallimage}', $smallimage, $template);
                $template = str_replace('{mediumimage}', $mediumimage, $template);
                $template = str_replace('{largeimage}', $largeimage, $template);
                $template = str_replace('{smallimagesrc}', $smallimagesrc, $template);
                $template = str_replace('{mediumimagesrc}', $mediumimagesrc, $template);               
                $template = str_replace('{largeimagesrc}', $largeimagesrc, $template);
                $template = str_replace('{technicaldetailsurl}', $technicaldetailsurl, $template);
                $template = str_replace('{customersreviewsurl}', $customersreviewsurl, $template);
                $template = str_replace('{salesrank}', $salesrank, $template);
                $template = str_replace('{detailbrand}', $detailbrand, $template);
                $template = str_replace('{featured}', $featured, $template);
                $template = str_replace('{listprice}', $listprice, $template);
                $template = str_replace('{offersurl}', $offersurl, $template);
                $template = str_replace('{productdescription}', $productdescription, $template);
                $template = str_replace('{relatedproducts}', $relatedproducts, $template);
			
                $template = str_replace('{asin}', $asin, $template);
                $template = str_replace('{urlasinrev}', $urlasinrev, $template);
				$template = str_replace('{urlasin}', $urlasin, $template);
				$template = str_replace('{asinrev}', strrev($asin), $template);
				
                $template = str_replace('{productdescriptionclean}', $productdescriptionclean, $template);
                $template = str_replace('{detailurlrev}', strrev($detailurl), $template);
                $template = str_replace('{mediumimagesrcrev}', strrev($mediumimagesrc), $template);
                $template = str_replace('{largeimagesrcrev}', strrev($largeimagesrc), $template);
                $template = str_replace('{technicaldetailsurlrev}', strrev($technicaldetailsurl), $template);
                $template = str_replace('{customersreviewsurlrev}', strrev($customersreviewsurl), $template);
                $template = str_replace('{offersurlrev}', strrev($offersurl), $template);
                $template = str_replace('{relatedproductsrev}', $relatedproductsrev, $template);
		
                

                //echo '<pre>';print_r($xml);echo '</pre>';
                //echo $template;
                //exit;
                $titleraws = preg_replace('/[^(\x20-\x7F)]*/', '', $title);
                $title = str_replace('{title}', $titleraws, $temp[0]);
                $template = preg_replace('/[^(\x20-\x7F)]*/', '', $template);
                $template = closetags($template);


                if (strpos($template, '{agcsnipet}') !== false)
                {
                    $agc_snipet = array();
                    $xml = fca_agc_snipet($titleraws);
                    $item = $xml->channel->item;
                    for ($i = 0; $i < (count($item)); $i++)
                    {
                        $agc_snipet[] = '<strong>' . $item[$i]->title . '</strong> ' . $item[$i]->
                            description;
                    }
                    shuffle($agc_snipet);
                    $agctext = implode(' ', $agc_snipet);
                    $agctext = trim(cleanchars($agctext));
                    $template = str_replace('{agcsnipet}', $agctext, $template);
                }

                $title = ParseSpinText($title);
                $template = ParseSpinText($template);
                $labels = fca_tag_yahoo($title . " " . $template, 6);

                $args = array('title' => $title, 'content' => $template, 'daterange' => trim($_POST['selectedrange']),
                    'labels' => $labels, 'project' => trim($_POST['project']));

                $write = xmlWriter($args);
                if ($write !== false)
                {
                    $asinlist = str_replace($search, '', $asinlist);
                    $asinlist = trim($asinlist);
                    $op = fopen($projectasin, "w");
                    fputs($op, $asinlist);
                    fclose($op);

                }
            } else
            {
                $asinlist = str_replace($search, '', $asinlist);
                $asinlist = trim($asinlist);
                $op = fopen($projectasin, "w");
                fputs($op, $asinlist);
                fclose($op);
                echo 'The ASIN got is not good! to promote, No product description found, just skipped it...<br />';
                $n--;
            }
        }
        if($next !== false) showReload();

    } else
    {

?>

<script type="text/javascript">
$(document).ready(function(){
    $('#daterange').DatePicker({
	flat: true,
	date: ['<?php

        echo date('Y-m-d', strtotime("-2 month", time()));

?>','<?php

        echo date('Y-m-d', strtotime("-1 month", time()));

?>','<?php

        echo date('Y-m-d', strtotime("+1 month", time()));

?>','<?php

        echo date('Y-m-d', strtotime("+2 month", time()));

?>'],
	current: '<?php

        echo date('Y-m-d');

?>',
	calendars: 5,
	mode: 'range',
	
    onChange: function(formated, dates){
		$("#selectedrange").val(formated);
		
	},
    starts: 0
    });
    $("#calender").scrollLeft(385);
    $("#expand").click(function(){
        $("#calender").css('width','1000px');
        $("#calender").css('overflow-y','none');
        $("#calender").css('position','absolute');
        $("#calender").css('left','-300px');
        $("#expand").css('display','none');
        $("#narrow").css('display','inline');
        $("#submit").css('position','absolute');
        $("#submit").css('top','311px');
        $("#submit").css('left','10px');
        
    });
    $("#narrow").click(function(){
        $("#calender").css('width','220px');
        $("#calender").css('overflow-y','scroll');
        $("#calender").css('position','static');
        $("#expand").css('display','inline');
        $("#narrow").css('display','none');
        $("#calender").scrollLeft(385);
        $("#submit").css('position','absolute');
        $("#submit").css('top','311px');
        $("#submit").css('left','10px');
    });
    $("#project").blur(function(){
        $("#project").val($("#project").val().replace(/ /g,"_"));
        $("#project").val($("#project").val().replace(/-/g,"_"));
    });
});
</script>

<div align="center">
<div class="xmlbuilder">
<form method="post">
Enter Project Name&nbsp;: <br /><input type="text" id="project" name="project" /> <br /><br />
Waiting Time&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: 
<select id="waiting" name="waiting">
<option <?php

        if ($waiting == 3)
            echo 'selected';

?> value="3">3</option>
<option <?php

        if ($waiting == 5)
            echo 'selected';

?> value="5">5</option>
<option <?php

        if ($waiting == 10)
            echo 'selected';

?> value="10">10</option>
<option <?php

        if ($waiting == 15)
            echo 'selected';

?> value="15">15</option>
<option <?php

        if ($waiting == 20)
            echo 'selected';

?> value="20">20</option> 
</select> secs<br />
ASIN Execution&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: 
<select id="exasin" name="exasin">
<option <?php

        if ($exasin == 3)
            echo 'selected';

?> value="3">3</option>
<option <?php

        if ($exasin == 5)
            echo 'selected';

?> value="5">5</option>
<option <?php

        if ($exasin == 10)
            echo 'selected';

?> value="10">10</option>
<option <?php

        if ($exasin == 15)
            echo 'selected';

?> value="15">15</option>
<option <?php

        if ($exasin == 20)
            echo 'selected';

?> value="20">20</option> 
<option <?php

        if ($exasin == 25)
            echo 'selected';

?> value="25">25</option> 
<option <?php

        if ($exasin == 30)
            echo 'selected';

?> value="30">30</option> 

</select> / session<br />
Date Range : 
<br />
<img id="expand" src="datepicker/expand.png"/><img id="narrow" src="datepicker/narrow.png" style="display:none"/>
<div class="calender" id="calender">
<div id="daterange"></div>
</div>
<input type="hidden" id="selectedrange" name="selectedrange" />
<input type="hidden" name="initial" value="1"/>
<input id="submit" type="submit" name="submit" value="submit" />

</form>
</div>
<div id="info">

</div>

</div>
<?php

    }
}
function xmlWriter($args)
{
    global $_POST;
    if (!is_dir('xml'))
        mkdir('xml');

    $title = $args['title'];
    $content = $args['content'];
    $daterange = $args['daterange'];
    $labels = $args['labels'];
    $project = $args['project'];

    $xmlproject = 'xml/' . $project . '.xml';
    if (isset($_POST['initial']) && file_exists($xmlproject))
    {
        unlink($xmlproject);
    }
    $title = htmlentities($title);
    $content = htmlentities($content);
    $date = explode(',', $daterange);
    $datepub = randDay($date[0], $date[1]);
    $timepub = randHours();

    if (file_exists($xmlproject))
    {
        $xmlstr = file_get_contents($xmlproject);
        $items = explode('<ns0:entry>', $xmlstr);
        $unit = count($items);
    }
    $numpost = 1;
    if (isset($unit))
        $numpost = $unit + 1;

    $itemxml = '<ns0:entry>';
    if ($labels != "")
    {
        $lab = explode(",", $labels);
        $lab = array_unique($lab);
        for ($i = 0; $i < count($lab); $i++)
        {
            if (isset($lab[$i]))
            {
                $labray = explode(" ", $lab[$i]);
                if (count($labray) < 3)
                {
                    $cleanlabel = preg_replace("/[^A-Za-z0-9?\s]/", "", $lab[$i]);
                    $itemxml .= '<ns0:category scheme="http://www.blogger.com/atom/ns#" term="' .
                        trim($cleanlabel) . '" />';
                }
            }
        }
    }
    $itemxml .= '<ns0:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/blogger/2008/kind#post" />';
    $itemxml .= '<ns0:id>post-' . $numpost .
        '</ns0:id><ns0:author><ns0:name>admin</ns0:name></ns0:author>';
    $itemxml .= '<ns0:content type="html">' . $content . '</ns0:content>';
    $itemxml .= '<ns0:published>' . $datepub . 'T' . $timepub . 'Z</ns0:published>';
    $itemxml .= '<ns0:title type="html">' . $title . '</ns0:title>';
    $itemxml .= '<ns1:control xmlns:ns1="http://purl.org/atom/app#"><ns1:published>yes</ns1:published></ns1:control>';
    $itemxml .= '<ns0:link href="http://' . $_SERVER['HTTP_HOST'] . '/?p=' . $numpost .
        '" rel="self" type="application/atom+xml" />';
    $itemxml .= '<ns0:link href="http://' . $_SERVER['HTTP_HOST'] . '/?p=' . $numpost .
        '" rel="alternate" type="text/html" />';
    $itemxml .= '</ns0:entry>';

    $oxm = fopen($xmlproject, "a");
    fputs($oxm, $itemxml);
    fclose($oxm);
    return true;


}
function randDay($start, $end)
{
    $start = strtotime($start);
    $end = strtotime($end);
    $daterand = rand($start, $end);
    $formateddate = date('Y-m-d', strtotime(date('Y-m-d', $daterand)));
    return $formateddate;
}
function randHours()
{
    $time = date('H:i:s');
    $starthours = strtotime('00:00:00');
    $endhours = strtotime('11:59:59');
    $randhours = rand($starthours, $endhours);
    $randtime = date('H:i:s', strtotime(date('H:i:s', $randhours)));
    return $randtime;
}
function showReload()
{
    global $_POST;

?>
<script type="text/javascript">
var Timer;
var TotalSeconds;
var stopMe = false;

function CreateTimer(TimerID, Time) {
  Timer = document.getElementById(TimerID);
  TotalSeconds = Time;
  
  UpdateTimer();
  window.setTimeout("Tick()", 1000);
}

function Tick() {
  if(TotalSeconds > 0  && stopMe == false)
  {
     TotalSeconds -= 1;
     UpdateTimer()
     window.setTimeout("Tick()", 1000);
  } else {
    if(stopMe == false) {
        send_data();
    }
  }
}

function UpdateTimer() {
   Timer.innerHTML = TotalSeconds;
}
function send_data()
{
    document.forms[0].submit();
}
function stopme()
{
    stopMe = true;
    UpdateTimer();
    window.location = window.location.href+"?getxml=<?php

    echo trim($_POST['project']);

?>";
}


</script> 
<?php

    $entry = 0;
    if (file_exists("xml/" . trim($_POST['project']) . ".xml"))
    {
        $xmlstr = file_get_contents("xml/" . trim($_POST['project']) . ".xml");
        $items = explode('<ns0:entry>', $xmlstr);
        $entry = count($items) - 1;
    }

?>
<div align="center">
<form method="post">
<input type="hidden" name="project" value="<?php

    echo trim($_POST['project']);

?>" />
<input type="hidden" name="selectedrange" value="<?php

    echo trim($_POST['selectedrange']);

?>" />

<input type="hidden" name="waiting" value="<?php

    echo trim($_POST['waiting']);

?>" />
<input type="hidden" name="exasin" value="<?php

    echo trim($_POST['exasin']);

?>" />
</form>
Created <?php

    echo $entry;

?> items, on XML DATA<br />
Waiting for <span id="timer"></span> Seconds to Execute next ASIN, <a href="javascript:stopme()">click here to stop</a><br />  
<script type="text/javascript">window.onload = CreateTimer("timer", <?php echo trim($_POST['waiting']); ?>);</script>
</div>
   <?php

}

?>
</body>
</html>